/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.sms.dao.StoreDiagnoseSettingDao;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseSetting;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseTemplate;
import com.chlitina.store.modules.sys.entity.Area;

/**
 * 店面诊断模板区域设置Service
 * @author liujiang
 * @version 2013-08-01
 */
@Component
@Transactional(readOnly = true)
public class StoreDiagnoseSettingService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreDiagnoseSettingService.class);
	
	@Autowired
	private StoreDiagnoseSettingDao storeDiagnoseSettingDao;
	
	public StoreDiagnoseSetting get(Long id) {
		return storeDiagnoseSettingDao.findOne(id);
	}
	
	public Page<StoreDiagnoseSetting> find(Page<StoreDiagnoseSetting> page, StoreDiagnoseSetting storeDiagnoseSetting) {
		DetachedCriteria dc = storeDiagnoseSettingDao.createDetachedCriteria();
		dc.addOrder(Order.desc("id"));
		return storeDiagnoseSettingDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreDiagnoseSetting storeDiagnoseSetting) {
		storeDiagnoseSettingDao.save(storeDiagnoseSetting);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeDiagnoseSettingDao.delete(id);
	}
	
	public StoreDiagnoseTemplate getTemplateByArea(Area area) {
		StoreDiagnoseTemplate result = null;
		while(result == null && area.getId() != 1) {
			DetachedCriteria dc = storeDiagnoseSettingDao.createDetachedCriteria();
			dc.add(Restrictions.eq("area", area));
			List<StoreDiagnoseSetting> list = storeDiagnoseSettingDao.find(dc);
			if(list.size() > 0) {
				result = list.get(0).getStoreDiagnoseTemplate();
			} else {
				area = area.getParent();
			}
		}
		return result;
	}
}  
